package com.hntest.controller;

import com.hntest.entity.Pager;
import com.hntest.entity.Student;
import com.hntest.service.impl.StudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller("studentController")
public class StudentController {
    @Autowired
    private StudentServiceImpl studentService;

    public void setStudentService(StudentServiceImpl studentService) {
        this.studentService = studentService;
    }

    @RequestMapping(value = "/select",method = {RequestMethod.POST,RequestMethod.GET})
    public String select(Pager pager, Student student, ModelMap modelMap){
        List<Student>list = studentService.getListByPage(pager,student);
        modelMap.put("list",list);
        modelMap.put("pager",pager);
        modelMap.put("stu",student);
        modelMap.put("gradeAll",studentService.getAllGrade());
        return "select";
    }

    @RequestMapping("/delete/{sno}")
    public String delete(@PathVariable(value = "sno") int sno){
        System.out.println("Controller 删除控制");
        int n = studentService.delete(sno);
        if (n > 0) {
            return "redirect:/select?op=delYes";
        }else {
            return "redirect:/select?op=delNo";
        }
    }

    @RequestMapping("/toInsert")
    public String toInsert(ModelMap modelMap){
        modelMap.put("gradeAll",studentService.getAllGrade());
        return "insert";
    }

    @RequestMapping("/doInsert")
    public String doInsert(Student student){
        int n=  studentService.insert(student);
        if (n>0){
            return "redirect:/select?op=insertYes";
        }else{
            return "redirect:/select?op=insertNo";
        }
    }

    @RequestMapping("/toUpdate/{sno}")
    public String toUpdaet(@PathVariable(value="sno") int sno,ModelMap modelMap){
        Student student = studentService.getStudentBySno(sno);
        modelMap.put("old",student);
        modelMap.put("gradeAll", studentService.getAllGrade());//年级下拉框
        return "update";
    }

    @RequestMapping("/doUpdate")
    public String doUpdate(Student student){
        int n=  studentService.update(student);
        if (n>0){
            return "redirect:/select?op=updateYes";
        }else{
            return "redirect:/select?op=updateNo";
        }
    }


}
